package library.core.book;

import java.io.IOException;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import library.database.user.DatabaseAdminProxy;
import library.utils.Info;

public class Book {
	
	private String name;
	private int id;
	private String author;
	private String type;
	private double price;
	private int num;

	/**
	 * 修改书籍信息
	 * @param name
	 * @param author
	 * @param type
	 * @param price
	 * @return
	 */
	public Info modifyBook(String name, String author, String type, String price) {
		try {
			this.name = name;
			this.author = author;
			this.type = type;
			this.price = Double.valueOf(price);
			DatabaseAdminProxy adminproxy = new DatabaseAdminProxy();
			if(name.length() > 255)
				return new Info("书名过长！");
			if(author.length() > 255)
				return new Info("作者名过长！");
			if(type.length()>255)
				return new Info("类型名过长！");
				if(price!=null){
					Pattern pattern = Pattern.compile("[0-9]+[.]{0,1}[0-9]*[dD]{0,1}");
					Matcher isDouble = pattern.matcher(price);
					if(!isDouble.matches())
						return new Info("价格输入格式错误！");		
			}
			if(Double.valueOf(price)<=0)
				return new Info("价格信息错误！");
			adminproxy.modifybook(1, name, this.id);
			adminproxy.modifybook(2, author, this.id);
			adminproxy.modifybook(3, type, this.id);
			adminproxy.modifybook(4, price, this.id);
			return Info.success("");
		} catch (ClassNotFoundException | IOException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Info("系统错误！");
		}
		
	}

	/**
	 * 更改书籍库存
	 * @param num
	 * @return
	 */
	public Info modifyBookNum(String num) {
		try {
		if(num!=null){
			boolean isNum = num.matches("[0-9]+");
			if(!isNum)
				return new Info("输入格式有误！");
		}
			if(Integer.valueOf(num)<0)
				return new Info("书籍数量不可少于0！");
			this.num = Integer.valueOf(num);
			DatabaseAdminProxy adminproxy = new DatabaseAdminProxy();
			adminproxy.modifybook(5,num,this.id);
			return Info.success("");

		} catch (ClassNotFoundException | IOException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Info("系统错误！");
		}

	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	
}
